package com.mrd.payClient;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mrd.payClient.message.PayMessage;
import com.mrd.payClient.message.ReconciliationMessage;
import com.mrd.payClient.message.RefundMessage;
import com.mrd.payClient.params.PayParams;
import com.mrd.payClient.params.ReconciliationParams;
import com.mrd.payClient.params.RefundParams;
import com.mrd.payClient.utils.Constents;
import com.mrd.payClient.utils.CoverUtils;
import com.mrd.payClient.utils.HttpClientUtils;
import com.mrd.payClient.utils.JsonBinder;
import com.mrd.payClient.utils.MD5Util;
import com.mrd.payClient.utils.ProperUtils;
import com.mrd.payClient.utils.Validate;

/**
 * 支付客户端
 * 
 * @author han
 */
public class PayClient implements IPayClient {

  /**
   * 支付接口
   * 
   * @author han
   * @param params PlayParams
   * @return PlayMessage
   */
  public PayMessage play(PayParams params) {
    String basePath = ProperUtils.getInstance().getPlayServerIp();
    String url = basePath + Constents.PLAYURL;
    Map map = new HashMap();
    PayMessage message = new PayMessage();
    try {
      String validResult = Validate.validateRequest(params);
      if (StringUtils.isEmpty(validResult)) {
        map = CoverUtils.getValue(params);
        String rtnVal = HttpClientUtils.doPost(url, map);
        JsonBinder binder = JsonBinder.buildNormalBinder();
        binder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        message = binder.fromJson(rtnVal, PayMessage.class);
      } else {
        message.setCode(-1);
        message.setMessage(validResult);
      }
    } catch (Exception ex) {
      message.setCode(-1);
      message.setMessage(Constents.FAULTMESSAGE);
    }

    return message;
  }

  /**
   * 退款接口
   * 
   * @author han
   * @param params RefundParams
   * @return RefundMessage
   */
  public RefundMessage refund(RefundParams params) {
    String basePath = ProperUtils.getInstance().getPlayServerIp();
    String url = basePath + Constents.REFUNDULR;
    Map map = new HashMap();
    RefundMessage message = new RefundMessage();
    try {
      String validResult = Validate.validateRequest(params);
      if (StringUtils.isEmpty(validResult)) {
        map = CoverUtils.getValue(params);
        String rtnVal = HttpClientUtils.doPost(url, map);
        JsonBinder binder = JsonBinder.buildNormalBinder();
        binder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        message = binder.fromJson(rtnVal, RefundMessage.class);
      } else {
        message.setCode(-1);
        message.setMessage(validResult);
      }
    } catch (Exception ex) {
      message.setCode(-1);
      message.setMessage(Constents.FAULTMESSAGE);
    }

    return message;
  }

  /**
   * 对账接口
   * 
   * @author han
   * @param params ReconciliationParams
   * @return ReconciliationMessage
   */
  public ReconciliationMessage reconciliation(ReconciliationParams params) {
    String basePath = ProperUtils.getInstance().getPlayServerIp();
    String url = basePath + Constents.RECONCILIATIONURL;
    Map map = new HashMap();
    ReconciliationMessage message = new ReconciliationMessage();
    try {
      String validResult = Validate.validateRequest(params);
      if (StringUtils.isEmpty(validResult)) {
        map = CoverUtils.getValue(params);
        String rtnVal = HttpClientUtils.doPost(url, map);
        JsonBinder binder = JsonBinder.buildNormalBinder();
        binder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        message = binder.fromJson(rtnVal, ReconciliationMessage.class);
      } else {
        message.setCode(-1);
        message.setMessage(validResult);
      }
    } catch (Exception ex) {
      message.setCode(-1);
      message.setMessage(Constents.FAULTMESSAGE);
    }

    return message;
  }

  public static void main(String[] args) {

    // 微信支付流程测试
    String outTradeNo =
        "tradePay" + +System.currentTimeMillis() + (long) (Math.random() * 10000000L);
    PayClient pc = new PayClient();

    // PayParams params = new PayParams();
    // params.setBody("test商品");
    // params.setPayCannel("1");
    // params.setOutTradeNo(outTradeNo);
    // params.setSubject("wxTestGood1");
    // params.setTotalAmount("1");
    // params.setTradeType("NATIVE");
    // params.setUserKey("mrdWeiXinPay");
    // params.setSign(MD5Util.getMD5(params.getUserKey() + params.getOutTradeNo()));
    // SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // String time2 = format2.format(new Date());
    // params.setTimeStart(time2);
    outTradeNo = "tradePayTest15989229584027329923";
    PayParams params = new PayParams();
    params.setBody("test商品");
    params.setPayChannel("1");
    params.setOutTradeNo(outTradeNo);
    params.setSubject("测试回掉通知信息");
    params.setTotalAmount("1");
    params.setTradeType("NATIVE");
    params.setUserKey("mrdWeiXinPay");
    params.setSign(MD5Util.getMD5(params.getUserKey() + params.getOutTradeNo()));
    SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time2 = format2.format(new Date());
    params.setTimeStart(time2);

    // test1No123347853
    // RefundParams params = new RefundParams();
    // params.setOutTradeNo("tradePay14979229584067309668");
    // params.setPayCannel("1");
    // params.setRefundAmount("1");
    // params.setTotalAmount("2");
    // params.setUserKey("mrdWeiXinPay");
    // params.setOutRefundNo(outTradeNo);
    // params.setSign(MD5Util.getMD5(params.getUserKey() + params.getOutTradeNo()));

    PayMessage msg = pc.play(params);
    // RefundMessage msg = pc.refund(params);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = format.format(new Date());
    System.out.println(time + "  " + msg);

    // System.out.println(time + msg);

  }
}
